/*=============================================================================
**                                  
**                      Proprietary - Copyright (C) 2013
**-----------------------------------------------------------------------------
** Supported MCUs      : <STM32F103 / MCU independent>
** Supported Compilers : <KeilC ARM/ compiler independent>
**-----------------------------------------------------------------------------
** File name         : io.h
**
** Module name       : io
**
**
** Summary: __________________________
**
**= History ====================================================================
** 1.0 31/03/2013  VietComp
** - Creation
==============================================================================*/
#include "io.h"

#define INPUT_LIMIT           TRIGGER_1_CHANNEL


#ifndef INPUT_LIMIT
#error "Input limit is not defined"
#endif
const uint16_t IOPin[IOm] = {
  BUT1_PIN,
  BUT2_PIN,
  BUT3_PIN,
	LCD_DETECT_PIN,
};
GPIO_TypeDef* IOPort[IOm] = {
  BUT1_PORT,
  BUT2_PORT,
  BUT2_PORT,
	LCD_DETECT_PORT,
};
const uint32_t IORCCPort[IOm] = {
  BUT1_CLK,
  BUT2_CLK,
  BUT3_CLK,
	LCD_DETECT_CLK,
};
/******************************************************************************
** Function name: GSM_ServiceInit()
** Description: This function shall be init  io pin
** Parameter X : PIN_SEL - pin selected
** Return value: None
** Remarks: global variables used, side effects
******************************************************************************/
void IO_init(InOutTypeDef PIN_SEL){
	GPIO_InitTypeDef GPIO_InitStructure;
	RCC_APB2PeriphClockCmd(IORCCPort[PIN_SEL], ENABLE );	
	GPIO_InitStructure.GPIO_Pin = IOPin[PIN_SEL];
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	//init input
	if(PIN_SEL<INPUT_LIMIT)
		GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
	else //init output
		GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; 
   GPIO_Init( IOPort[PIN_SEL], &GPIO_InitStructure );

}
/******************************************************************************
** Function name: IO_read()
** Description: This should be set bit on PIN
** Parameter X : PIN_SEL - pin selected
** Return value: value of pin 0 or 1
** Remarks: global variables used, side effects
******************************************************************************/
uint8_t IO_read(InOutTypeDef PIN_SEL){
	if(PIN_SEL<INPUT_LIMIT)
		return (GPIO_ReadInputDataBit(IOPort[PIN_SEL],IOPin[PIN_SEL]));
	else
		 return IO_NULL;
}
/******************************************************************************
** Function name: IO_set()
** Description: This should be set bit on PIN
** Parameter X : PIN_SEL - pin selected
** Return value: None
** Remarks: global variables used, side effects
******************************************************************************/
void IO_set(InOutTypeDef PIN_SEL)
{
    if(PIN_SEL>INPUT_LIMIT && PIN_SEL <  IO_LIMIT)
        GPIO_SetBits(IOPort[PIN_SEL],IOPin[PIN_SEL]);
    else
        /* do nothing */
        ;
}
/******************************************************************************
** Function name: IO_clr()
** Description: This should be clear bit on PIN
** Parameter X : PIN_SEL - pin selected
** Return value: None
** Remarks: global variables used, side effects
******************************************************************************/
void IO_clr(InOutTypeDef PIN_SEL)
{
    if(PIN_SEL>INPUT_LIMIT && PIN_SEL <= IO_LIMIT)
        GPIO_ResetBits(IOPort[PIN_SEL],IOPin[PIN_SEL]);
    else
        /* do nothing */
        ; 
}

